// The functions contained in this file are pretty dummy
// and are included only as a placeholder. Nevertheless,
// they *will* get included in the shared library if you
// don't remove them :)
//
// Obviously, you 'll have to write yourself the super-duper
// functions to include in the resulting library...
// Also, it's not necessary to write every function in this file.
// Feel free to add more files in this project. They will be
// included in the resulting library.

extern "C++"
{
    #include "mGlobals.h"
    //adds number in the end and greates new director.. oututs it name
    std::string newDir( const char * name )
	{
	//system(code.c_str());

        std::string dir;
        for(int a=0;a<1000;a++)
        {
            dir = name;
            std::ostringstream convert;
            convert << a;
            dir += convert.str();
            if( mkdir(dir.c_str(),S_IRWXU|S_IRGRP|S_IXGRP) == 0)break;
        }

        return dir;
	}
	std::string newFile( const char * start , ...)
	{
	//system(code.c_str());

        std::string order = start;
        std::string next;

        va_list vl;
        va_start(vl,start);
        while(true)
        {
            next = va_arg(vl,const char *);
            if(next=="END")break;
            order += next;
        }
        va_end(vl);

        std::string dir;
        for(int a=0;a<1000;a++)
        {
            dir = order;
            std::ostringstream convert;
            convert << a;
            dir += convert.str();


            std::ifstream ifile( dir.c_str() );
            if (!ifile)break;
            //if( mkdir(dir.c_str(),S_IRWXU|S_IRGRP|S_IXGRP) == 0)break;
        }

        return dir;
	}


    std::string mS( const char * start , ...)
	{
        std::string order = start;
        std::string next;

        va_list vl;
        va_start(vl,start);
        while(true)
        {
            next = va_arg(vl,const char *);
            if(next=="END")break;
            order += next;
        }
        va_end(vl);

        return order;
	}

    //file updated?
	bool FileIsUpdated(int *TimeOfUpdate, std::string sim )
	{
        struct stat st;
        int ierr = stat (sim.c_str(), &st);
        if (ierr != 0) {
            return false;
        }
        if(*TimeOfUpdate != st.st_mtime)
        {
            *TimeOfUpdate = st.st_mtime;
            return true;
        }
        return false;
	}

	//*************** change  ********//
	bool LineChange(std::string filename,uint row,std::string dta)
	{
        std::vector<std::string> rows;
        std::ifstream codefile(filename.c_str());
        if (codefile.is_open())
        {
            std::string line;
            while(getline(codefile,line))rows.push_back(line);

            codefile.close();
            if(rows.size()<row)return false;
            rows[row] = dta;

            std::ofstream myfile;
            myfile.open(filename.c_str());
            if (myfile.is_open())
            {
                for(uint b=0; b<rows.size(); b++)myfile << rows[b] << "\n";
                myfile.close();
            }
        }
        return true;
	}
	//*************** change  ********//
	std::string LineRead(std::string filename,uint row)
	{
        std::ifstream codefile(filename.c_str());
        if (codefile.is_open())
        {
            std::string line;
            uint a=0;
            while(getline(codefile,line))
            {
                if(a==row) return line;
                a++;
            }
            codefile.close();
            return "";
        }
        return "";
	}
	//**************** CLEAR PROJECT
	bool SaveProject()
	{

	    system( ( "cd " + LineRead("prefs",1) + "\ntar -cf " + LineRead("prefs",0) + " *\n" ).c_str() ); //, "END" );
        return true;

	}


	//**************** CLEAR PROJECT
	bool ClearProject()
	{
         system( ( "rm -rf " + LineRead("prefs",1) + "\n").c_str() );
         return true;
	}
}
